package com.chengzzz.czsee.core

import android.content.pm.ActivityInfo
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.chengzzz.czsee.R
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.utils.OrientationUtils
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import kotlinx.android.synthetic.main.activity_play.*


class PlayActivity : AppCompatActivity() {
    var videoPlayer: StandardGSYVideoPlayer? = null

    var orientationUtils: OrientationUtils? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

        setContentView(R.layout.activity_play)
        val url = intent.getStringExtra("url")
        val name = intent.getStringExtra("name")
        init(url as String,name as String)
    }
    private fun init(url:String,name: String) {
        Toast.makeText(this, "正在加载...", Toast.LENGTH_SHORT).show()

        val source1 =
            "http://meng.wuyou-zuida.com/20191126/22804_82983b3c/index.m3u8"
//        val source1 =
//            url
//        Log.d("TAG", "urlinit: "+url.trim().length+"--"+url)
//        Log.d("TAG", "source1init: "+source1.length+"--"+source1)
        detail_player.setUp(url.trim(), true, name)

        //增加封面
        val imageView = ImageView(this)
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP)
        imageView.setImageResource(R.mipmap.ic_launcher)
        detail_player.setThumbImageView(imageView)
        //增加title
        detail_player.getTitleTextView().setVisibility(View.VISIBLE)
        //设置返回键
        detail_player.getBackButton().setVisibility(View.VISIBLE)
        //设置旋转
        orientationUtils = OrientationUtils(this, detail_player)
        //设置全屏按键功能,这是使用的是选择屏幕，而不是全屏
        detail_player.getFullscreenButton().setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                orientationUtils!!.resolveByClick()
            }
        })
        //是否可以滑动调整
        detail_player.setIsTouchWiget(true)
        //设置返回按键功能
        detail_player.getBackButton().setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                onBackPressed()
            }
        })
        detail_player.startPlayLogic()
    }


    override fun onPause() {
        super.onPause()
        detail_player.onVideoPause()
    }

    override fun onResume() {
        super.onResume()
        detail_player.onVideoResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        GSYVideoManager.releaseAllVideos()
        orientationUtils?.releaseListener()
    }

    override fun onBackPressed() {
        //先返回正常状态
        if (orientationUtils!!.getScreenType() === ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            detail_player.getFullscreenButton().performClick()
            return
        }
        //释放所有
        detail_player.setVideoAllCallBack(null)
        super.onBackPressed()
    }
}